package com.huaxin.hxcloud.work;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.huaxin.common.BaseActivity;
import com.huaxin.common.Entity.HxScSyncDataDTO;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.hxcloud.R;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author songxf
 * @date 2018/5/21
 * @Desc 首创综合数据
 * Copyright HUAXIN DATA. All rights reserved.
 */
public class HxScSyncDataActivty extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar      mToolbar;
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerview;

    @BindView(R.id.content)
    FrameLayout mContent;
    private HxScSyncDataAdapter mHxScSyncDataAdapter;

    //private List<HxScSyncDataDTO.InfosBean> mFlag;
    private HxScSyncDataDTO mSyncDataDTO;
    private static final String TAG = "HxScSyncDataActivty";
    private TextView           mTitleTv;
    private RecyclerView       mOrgRecyclerview;
    private HxScSyncOrgAdapter mOrgAdapter;

    @Override
    protected int getContentViewId() {
        return R.layout.activty_sc_sync_data;
    }

    @Override
    protected int getLoadFrameId() {
        return R.id.content;
    }

    @Override
    public void initView() {
        mSyncDataDTO = new HxScSyncDataDTO();
        setSupportActionBar(mToolbar);
        mToolbar.setTitle(getResources().getString(R.string.module_synthesize_data));
        //站点 适配器
        mHxScSyncDataAdapter = new HxScSyncDataAdapter(this);
        //Org 适配器
        mOrgAdapter = new HxScSyncOrgAdapter();
        View headerView = getLayoutInflater().inflate(R.layout.item_sc_sync_data, (ViewGroup) mRecyclerview.getParent(), false);
        mTitleTv = (TextView) headerView.findViewById(R.id.title_tv);
        mOrgRecyclerview = (RecyclerView) headerView.findViewById(R.id.recyclerview);
        mOrgRecyclerview.setAdapter(mOrgAdapter);

        mOrgRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mHxScSyncDataAdapter.addHeaderView(headerView);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerview.setAdapter(mHxScSyncDataAdapter);

    }

    @Override
    protected void requestData() {
        updateLoadStatus(LOADING);
        ApiRequsetData();

    }

    private void ApiRequsetData() {
        Call<HxScSyncDataDTO> call = HxTransmitRequestHelper.getRequestApi().getSPTAnalysis("2018");
        autoClose(call);
        call.enqueue(new Callback<HxScSyncDataDTO>() {
            @Override
            public void onResponse(Call<HxScSyncDataDTO> call, Response<HxScSyncDataDTO> response) {
                HxScSyncDataDTO syncDataDTO = response.body();
                if (response.isSuccessful()) {
                    if (syncDataDTO.getData() == null) {
                        updateLoadStatus(LOAD_NO_DATA);
                        return;
                    }
                    if (response.isSuccessful()) {
                        updateLoadStatus(LOAD_SUCCESS);
                        //OrgList
                        HxScSyncDataDTO.DataBean dataBean = syncDataDTO.getData();
                        mTitleTv.setText((dataBean.getOrg().getOrgName() + ""));
                        mOrgAdapter.addData(dataBean.getOrg().getProcess());
                        mHxScSyncDataAdapter.addData(dataBean.getOc());

                    }
                } else {
                    updateLoadStatus(LOAD_ERROR);
                }

            }

            @Override
            public void onFailure(Call<HxScSyncDataDTO> call, Throwable t) {
                updateLoadStatus(LOAD_FAILURE);
            }
        });

    }

}
